package com.czf.testmodule.annotation;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author zfChen
 * @create 2022/7/4 14:53
 */
@Component
@Aspect
@Slf4j
public class LogRqRpAspect {

    @Pointcut("@annotation(LogRqRp)")
    public void pointCut(){}

    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        LogRqRp annotation = signature.getMethod().getAnnotation(LogRqRp.class);
        String value = annotation.value();
        log.info("【{}】的入参为：{}", value, JSON.toJSONString(joinPoint.getArgs()));
    }

    @AfterReturning(value = "pointCut()",returning = "obj")
    public void afterReturning(JoinPoint joinPoint,Object obj){
        if (ObjectUtils.allNotNull(obj)) {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            LogRqRp annotation = signature.getMethod().getAnnotation(LogRqRp.class);
            String value = annotation.value();
            log.info("【{}】的返回值为：{}", value, JSON.toJSONString(obj));
        }
    }
}
